let http = require('http');
let fs = require('fs');

let server = http.createServer();

let wwwDir = './www';

// 像apache服务器一样 可以通过网址来访问资源
server.on('request', function (req, res) {
  let url = req.url;

  let filePath = '/index.html';

  if (url !== '/') {
    filePath = url;
  }

  fs.readFile(wwwDir + filePath, function (err, data) {
    if (err) {
      return res.end('404 Not Found');
    }
    res.end(data);
  });
});

// 3.绑定端口号,启动服务
server.listen(3000, function () {
  console.log('running');
});
